import { computed } from "@preact/signals"
import kycController from "@/features_signal/kycController"
import { createLogger } from "@/utils/logger"
import { StepComponentMap } from "@/components/kyc/steps"

const logger = createLogger("KycPageSignals")

const CurrentStepComponent = computed(() => {
    logger.debug("CurrentStepComponent", kycController.currentStage.value)
    return StepComponentMap[kycController.currentStage.value.step]
})

const formDefaultValues = computed(() => {
    const sourceData = kycController.sourceData.value
    const cachedFormData = kycController.cachedFormData.peek()
    return {
        ...cachedFormData,
        ...sourceData.kyc,
        // 真實姓名
        first_name: sourceData.kyc?.first_name || cachedFormData?.first_name,
        middle_name: sourceData.kyc?.middle_name || cachedFormData?.middle_name,
        last_name: sourceData.kyc?.last_name || cachedFormData?.last_name,
        // 最近分行
        nearest_branch: sourceData?.kyc?.nearest_branch || "2042 Taft Ave, Pasay, Metro Manila",
        nationality: sourceData?.kyc?.nationality || "Philippines",
        // 出生日期
        birth_date: sourceData?.kyc?.birthday || cachedFormData?.birthday,
        // 出生地
        birth_place: sourceData?.kyc?.place_of_birth || cachedFormData?.place_of_birth,
        // 電話
        phone: sourceData.profile?.phone,
        // 自拍
        attachments_face: sourceData?.kyc?.attachments_face,
        // 身分證照片
        attachments_idPhoto: sourceData?.kyc?.attachments_idPhoto,
        // 自拍身分證照片
        attachments_selfieWithIDPhotoPath:
            sourceData?.kyc?.attachments_selfieWithIDPhotoPath ||
            cachedFormData?.attachments_selfieWithIDPhotoPath ||
            "",
    }
})

const formDisabled = computed(() => {
    const sourceData = kycController.sourceData.value
    const steps: Kyc.Status[] = ["pending", "final_rejected"]
    return steps.includes(sourceData.kyc?.status || "unset")
})

const KycPageSignals = {
    CurrentStepComponent,
    formDefaultValues,
    formDisabled,
    ...kycController,
}

export default KycPageSignals
